package com.kcyu.sfherp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kcyu.sfherp.entity.Chmx;
import com.kcyu.sfherp.entity.Chzd;
import com.kcyu.sfherp.entity.utilEntity.FilterDTO;
import com.kcyu.sfherp.entity.utilEntity.PageEntity;
import com.kcyu.sfherp.mapper.ms.ChmxMapper;
import com.kcyu.sfherp.mapper.ms.ChzdMapper;
import com.kcyu.sfherp.service.ChzdService;
import com.kcyu.sfherp.service.StatisticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class StatisticServiceImpl implements StatisticService {

    @Autowired
    private ChzdMapper chzdMapper;

    @Autowired
    private ChmxMapper chmxMapper;

    @Override
    public List<Chzd> queryByConditions(FilterDTO filterDTO) {
        QueryWrapper<Chzd> wrapper = new QueryWrapper<>();
        wrapper.like("carman",filterDTO.getCarman());
        wrapper.ge("rq", filterDTO.getStartTime());
        wrapper.le("rq", filterDTO.getEndTime());
        List<Chzd> chzds = chzdMapper.selectList(wrapper);

        chzds.forEach(chzd -> {
            String dh = chzd.getDh();
            QueryWrapper<Chmx> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("dh", dh);
            List<Chmx> chmxes = chmxMapper.selectList(wrapper1);
            Float totalWeight = 0f;
            for (Chmx chmx : chmxes) {
                float v = chmx.getNetweight() * chmx.getSl();
                totalWeight += v;
            }

            chzd.setTotalWeight(totalWeight);
        });

        return chzds;
    }

    @Override
    public PageEntity<Chzd> queryByConditions2(PageEntity<Chzd> pageEntity) {

        PageEntity<Chzd> chzdPageEntity = new PageEntity<>();
        chzdPageEntity.setQueryCondition(pageEntity.getQueryCondition());

        // 先查明细
        QueryWrapper<Chmx> wrapper = new QueryWrapper<>();
        wrapper.like("carman", pageEntity.getQueryCondition().getCarman());
        wrapper.ge("rq", pageEntity.getQueryCondition().getStartTime());
        wrapper.le("rq", pageEntity.getQueryCondition().getEndTime());
        List<Chmx> chmxes = chmxMapper.selectList(wrapper);

        Float totalWeight = 0f;
        for (Chmx chmx : chmxes) {
            float v = chmx.getNetweight() * chmx.getSl();
            totalWeight += v;
        }

        chzdPageEntity.setTotalWeight(totalWeight);

        // 再查主单
        QueryWrapper<Chzd> wrapper1 = new QueryWrapper<>();
        wrapper1.like("carman", pageEntity.getQueryCondition().getCarman());
        wrapper1.ge("rq", pageEntity.getQueryCondition().getStartTime());
        wrapper1.le("rq", pageEntity.getQueryCondition().getEndTime());
        List<Chzd> chzds = chzdMapper.selectList(wrapper1);

        Float totalJe = chzds.stream().map(Chzd::getJe).reduce(0f, Float::sum);
        chzdPageEntity.setTotalMoney(totalJe);
        chzdPageEntity.setTotal((long) chzds.size());
        chzdPageEntity.setRecords(chzds);
        return chzdPageEntity;
    }
}
